<template>
<el-dialog :visible.sync="visible" :title="title" v-if="visible">
  <el-form :model="formData" label-position="right" label-width="80px" :rules="rules" ref="formData">
    <el-form-item label="分类名称" prop="categoryName">
      <el-input placeholder="请输入分类名称" v-model="formData.categoryName">
      </el-input>
    </el-form-item>
    <el-form-item label="状态">
      <el-radio-group v-model="formData.status">
        <el-radio :label="1">可用</el-radio>
        <el-radio :label="2">禁用</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="update">提交</el-button>
      <el-button type="warning" @click="visible = false">取消</el-button>
    </el-form-item>
  </el-form>
</el-dialog>
</template>

<script>
const formData = {
  categoryId: '',
  categoryName: '',
  status: 1
}
export default {
  name: 'categoryUpdate',
  data () {
    return {
      visible: false,
      title: '添加数据',
      formData: {
        ...formData
      },
      rules: {
        categoryName: [{
          required: true,
          message: '请输入公告内容',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    init (categoryId) {
      this.visible = true
      this.formData = { ...formData }
      if (categoryId) {
        this.title = '修改数据'
        this.$http.get(`category/info/${categoryId}`).then(({ data }) => {
          this.formData = { ...data }
        })
      }
    },
    update () {
      this.$refs.formData.validate(valid => {
        if (valid) {
          this.$http.post('category/update', this.formData).then(() => {
            this.$alert(`${this.formData.categoryId ? '修改' : '添加'}成功`, { type: 'success' }).then(() => {
              this.visible = false
              this.$emit('onCloseDialog')
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
